/*
 * @Author: GY19531 418941369@qq.com
 * @Date: 2022-08-30 15:53:47
 * @LastEditors: GY19531 418941369@qq.com
 * @LastEditTime: 2022-09-09 11:07:11
 * @FilePath: \one-web\src\pages\RemindManage\mview\Year.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { useState, useEffect } from 'react'
import { Radio, InputNumber, message, Col, List } from 'antd'
const { Group } = Radio
import { isNumber } from '../common/isNumber'
export default function Year(props: any) {
  function changeParams(type: string, value: any) {
    const state = { ...props.year }
    state[type] = value
    if (type === 'start') {
      if (state.end - state.start <= 1) {
        state.end = value + 1
      }
    }
    if (type === 'end') {
      if (state.end - state.start <= 1) {
        state.start = value - 1
      }
    }
    props.onchange(state)
  }
  const {
    year: { type, start, end },
  } = props
  return (
    <div>
      <Group
        value={type}
        onChange={(e) => {
          changeParams('type', e.target.value)
        }}
        defaultValue=""
        style={{ width: "100%" }}
      >
        <List size="small" bordered>
          <List.Item>
            <Radio value="">不指定</Radio>
          </List.Item>
          <List.Item>
            <Radio value="*">每年</Radio>
          </List.Item>
          <List.Item>
            <Radio value="period">周期</Radio>
            <InputNumber
              min={new Date().getFullYear()}
              value={start}
              placeholder="年"
              formatter={(value) => value.toString().replace(/[^\d\.]/g, '')}
              onChange={(value) => {
                if (isNumber(value) && Number(value) >= new Date().getFullYear()) {
                  changeParams('start', value)
                } else {
                  message.info('输入不合法')
                }
              }}
              disabled={type !== 'period'}
            />
            {' - '}
            <InputNumber
              min={new Date().getFullYear() + 1}
              value={end}
              placeholder="年"
              formatter={(value) => value.toString().replace(/[^\d\.]/g, '')}
              onChange={(value) => {
                if (isNumber(value) && Number(value) >= new Date().getFullYear() + 1) {
                  changeParams('end', value)
                } else {
                  message.info('输入不合法')
                }
              }}
              disabled={type !== 'period'}
            />
          </List.Item>
        </List>
      </Group>
    </div>
  )
}
